package geektime.spring.Remote.controller;

import geektime.spring.springbucks.model.Coffee;
import geektime.spring.springbucks.model.CoffeeOrder;
import geektime.spring.springbucks.model.OrderState;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.hateoas.Resource;
import org.springframework.web.util.UriComponentsBuilder;

import org.springframework.beans.factory.annotation.Value;

import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@Slf4j
public class CoffeeOrderController {
    @Autowired
    private RestTemplate restTemplate;
    @Value("${geektime.spring.springbucks.service.url}")
    private String url;

    @RequestMapping(value = "/getCoffeeJsons",method = RequestMethod.GET)
    @ResponseBody
    private List<Coffee> getCoffeeJsons() {
        Coffee[] coffee= restTemplate.getForObject(url + "/getCoffeeJson/", Coffee[].class);
        log.info("结果,{}",Arrays.asList(coffee));
        return Arrays.asList(coffee);
    }

    @RequestMapping(value = "/getByNameXmls/{name}",method = RequestMethod.GET,produces = MediaType.APPLICATION_XML_VALUE)
    @ResponseBody
    private Coffee getByNameXmls(@PathVariable String name) {
        String uri = url+"/getByNameXml/"+name;
        Coffee coffee= restTemplate.getForObject(uri, Coffee.class);
        log.info("结果,{}",coffee);
        return coffee;
    }


}
